//
// Created by hay boy on 2022/6/23.
//

#ifndef ALGORITHMS_UNITCHAIN_H
#define ALGORITHMS_UNITCHAIN_H


struct equivNode{
    int equivClass,size,next;
    //分别为元素标识符，类中的元素个数，类中指向下一个元素的指针（整形指针，也称模拟的指针）。

};
equivNode* node;
int n;
void initialize(int numberofElements){
    n = numberofElements;
    node = new equivNode[n+1];
    for(int e = 1;e<=n;e++){
        node[e].equivClass = e;
        node[e].next = 0;
        node[e].size = 1;

    }
}
void unite(int classA,int classB){
    if(node[classA].size<node[classB].size){
        int tmp = classA;
        classA = classB;
        classB = tmp;
    }
    int k;
    for(k=classA;node[k].next!=0;k=node[k].next){
        node[k].equivClass = classB;
    }
    node[k].equivClass = classB;
    node[classB].size+=node[classA].size;
    node[k].next = node[classB].next;
    node[classB].next = classA;
}
int find(int theElement){
    return node[theElement].equivClass;
}
#endif //ALGORITHMS_UNITCHAIN_H
